package networking;

import ecologylab.collections.Scope;
import ecologylab.oodss.distributed.server.clientsessionmanager.SessionHandle;
import ecologylab.oodss.messages.UpdateMessage;
import ecologylab.serialization.ElementState.simpl_scalar;

public class ChatUpdate extends UpdateMessage
{
  @simpl_scalar
  private String  message;

  @simpl_scalar
  private String  host;

  @simpl_scalar
  private int    port;

  /**
   * Constructor used on client. Fields populated automatically by
   * s.im.pl serialization
   */
  public ChatUpdate()
  {
  }

  /**
   * Constructor used on server
   * 
   * @param message
   *           the chat message
   * 
   * @param handle
   *           handle of originating client
   */
  public ChatUpdate(String message, SessionHandle handle)
  {
    this.message = message;
    this.host = handle.getInetAddress().toString();
    this.port = handle.getPortNumber();
  }

  /**
   * Called automatically by OODSS on client
   */
  @Override
  public void processUpdate(Scope appObjScope)
  {
    /* get the chat listener */
    ChatUpdateListener listener = (ChatUpdateListener) appObjScope
        .get(ChatUpdateListener.CHAT_UPDATE_LISTENER);

    /* report incoming update */
    listener.recievedUpdate(this);
  }

  public String getMessage()
  {
    return message;
  }

  public String getHost()
  {
    return host;
  }

  public int getPort()
  {
    return port;
  }
}
